package com.lanou.sinasports.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.sinasports.R;
import com.lanou.sinasports.adapter.SpecialAdapter;
import com.lanou.sinasports.beans.GsonSpecial;
import com.lanou.sinasports.beans.Special;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by dllo on 16/1/7.
 */
public class SpecialFragment extends Fragment {

    private ListView listView;
    private SpecialAdapter adapter;
    private Context context;
    private List<Special> data;
    private RequestQueue queue;
    private ImageView ivAdd;

    private String url="http://platform.sina.com.cn/sports_client/z_list";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.fragment_special,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        listView= (ListView) view.findViewById(R.id.lv_special);
        ivAdd= (ImageView) view.findViewById(R.id.iv_special_top);

    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showShare();

            }
        });
        queue= Volley.newRequestQueue(context);
        data=new ArrayList<>();

        StringRequest request=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson=new Gson();
                GsonSpecial gsonSpecial=gson.fromJson(response,GsonSpecial.class);

                int size=gsonSpecial.getResult().getData().getList().size();

                for(int i=0;i<size;i++){
                    String title=gsonSpecial.getResult().getData().getList().get(i).getTitle();
                    String desc=gsonSpecial.getResult().getData().getList().get(i).getDesc();
                    String pic=gsonSpecial.getResult().getData().getList().get(i).getPic();
                    String id=gsonSpecial.getResult().getData().getList().get(i).get_id();
                    int num=gsonSpecial.getResult().getData().getList().get(i).getFans_num();

                    Special special=new Special();
                    special.setTitle(title);
                    special.setDesc(desc);
                    special.setPic(pic);
                    special.setId(id);
                    special.setNum(num);

                    Log.d("SpecialFragment", gsonSpecial.getResult().getData().getList().get(i).getTitle());
                    data.add(special);
                }


                adapter=new SpecialAdapter(context,data);
                listView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        })
        {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                 Map<String,String> headers=new HashMap<>();
                 headers.put("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
                 headers.put("User-Agent","Dalvik/1.6.0 (Linux; U; Android 4.4.4; MI 4LTE MIUI/V7.1.3.0.KXDCNCK) Paros/3.2.13");
                 headers.put("Host","platform.sina.com.cn");
                 headers.put("Connection","Keep-Alive");
                 headers.put("Content-Length","69");
                return super.getHeaders();
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> headers=new HashMap<>();
                headers.put("app_key","2586208540");
                headers.put("_version__","3.2.0.14");
                headers.put("__os__","android");
                headers.put("list_type","recmd");

                return headers;
            }
        };

         queue.add(request);


    }
    private void showShare() {
        ShareSDK.initSDK(getActivity());
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("1111");
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(getActivity());
    }

}
